package dst.benchmark;

import dataStructures.list.SinglelyLinkedArray;
import dataStructures.list.SinglelyLinkedList;
import java.util.ArrayList;

/**
 * A class for benchmarking the performance of java.util.ArrayList,
 * MyUtil.dataStructures.list SinglelyLinkedList and SinglelyLinkedArray
 *
 * Fully Tested: No
 * Fully Documented: No
 * Created: 22-Nov-2011
 * Last Updated: 22-Nov-2011
 *
 * @author Shimu Wu
 */
public class ListBenchMarking {

    private static class ListBenchMarker implements BenchMarkable {

        private int totalElements;

        private ListBenchMarker(int totalElements) {
            this.totalElements = totalElements;
        }

        public void execute() {
            SinglelyLinkedArray<Integer> list =
                    new SinglelyLinkedArray<Integer>();
            for (int i = 0; i < totalElements; i++) {
                list.addLast(i);
            }
            list = null;
        }

        public String getDescription() {
            return "Inserting " + totalElements
                    + " elements into Unrolled linked list";
        }
    }

    private static class ArrayListBenchMarker implements BenchMarkable {

        private int totalElements;

        private ArrayListBenchMarker(int totalElements) {
            this.totalElements = totalElements;
        }

        public void execute() {
            ArrayList<Integer> list = new ArrayList<Integer>();
            for (int i = 0; i < totalElements; i++) {
                list.add(i);
            }
            list = null;
        }

        public String getDescription() {
            return "Inserting " + totalElements + " elements into Arraylist";
        }
    }

    private static class LinkedListBenchMarker implements BenchMarkable {

        private int totalElements;

        private LinkedListBenchMarker(int totalElements) {
            this.totalElements = totalElements;
        }
        
        public void execute() {
            SinglelyLinkedList<Integer> list = new SinglelyLinkedList<Integer>();
            for (int i = 0; i < totalElements; i++) {
                list.addLast(i);
            }
            list = null;
        }

        public String getDescription() {
            return "Inserting " + totalElements + 
                    " elements into Singlely Linked List";
        }
        
    }
    
    public static void main(String[] args) {

        int totalElements = 500000;
        int iteration = 100;
        ListBenchMarker b1 = new ListBenchMarker(totalElements);
        ArrayListBenchMarker b2 = new ArrayListBenchMarker(totalElements);
        LinkedListBenchMarker b3 = new LinkedListBenchMarker(totalElements);
        
        BenchMarker benchMarker = new BenchMarker();

        benchMarker.benchMark(b1, iteration);
        benchMarker.benchMark(b2, iteration);
        benchMarker.benchMark(b3, iteration);
        benchMarker.benchMark(b1, iteration);
        benchMarker.benchMark(b2, iteration);
        benchMarker.benchMark(b3, iteration);

    }
}
